﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using GMS.Dinasty.BO;

namespace GMS.BasicControls
{
    public partial class CalculateDimensionsA : BackGroundControl
    {
        private DataTable dimensionTable;
        public CalculateDimensionsA()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
        }

        private List<DimensionsA> _dims;
        public List<DimensionsA> Dims
        {
            get { return _dims; }
            set { _dims = value; }
        }

        private void CalculateDimensionsA_Load(object sender, EventArgs e)
        {
            dimensionTable = new DataTable();
            DataColumn dc = new DataColumn("widthColumn");
            dimensionTable.Columns.Add(dc);
            dc = new DataColumn("heightColumn");
            dimensionTable.Columns.Add(dc);
            dc = new DataColumn("quantityColumn");
            dimensionTable.Columns.Add(dc);
            dc = new DataColumn("squareColumn");
            dimensionTable.Columns.Add(dc);
            foreach (DimensionsA dim in Dims)
            {
                DataRow dr = dimensionTable.NewRow();
                dr["widthColumn"] = dim._width;
                dr["heightColumn"] = dim._height;
                dr["quantityColumn"] = dim._quantity;
                //dr["squareColumn"] = dim.Square();
                dr["squareColumn"] = Math.Round(double.Parse(dr["widthColumn"].ToString())/1000 * double.Parse(dr["heightColumn"].ToString())/1000 * double.Parse(dr["quantityColumn"].ToString()),3);
                dimensionTable.Rows.Add(dr);
            }
            dataGridView1.DataSource = dimensionTable.DefaultView;
            this.BringToFront();
        }

        private void saveDimensionsLable_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Dims.Clear();
            if (dimensionTable.Rows.Count > 0)
            {
                foreach (DataRow dr in dimensionTable.Rows)
                {
                    DimensionsA dim = new DimensionsA(int.Parse(dr["widthColumn"].ToString()), int.Parse(dr["heightColumn"].ToString()), int.Parse(dr["quantityColumn"].ToString()));
                    Dims.Add(dim);
                }
            }
            closeLabel_LinkClicked(sender, e);
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            double width = 0, height = 0, quantity = 0;
            double square = 0;
            if (double.TryParse(dataGridView1.CurrentRow.Cells["widthColumn"].Value.ToString(), out width) && double.TryParse(dataGridView1.CurrentRow.Cells["heightColumn"].Value.ToString(), out height) && double.TryParse(dataGridView1.CurrentRow.Cells["quantityColumn"].Value.ToString(), out quantity))
            {
                square = (width * height * quantity) / 1000000;
                square = Math.Round(square, 3);
            }
            dataGridView1.CurrentRow.Cells["squareColumn"].Value = square;
        }
    }
}
